package com.os.web.service;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.async.DeferredResult;

/**
 * @Author Matt
 * @Date 2022-01-23
 * @Desc:
 * @Version V1.0
 */
@Service
public class PushService {

    private DeferredResult<String> deferredResult;  //产生deferredResult给控制器使用，通过@scheduled注解方法定时更新结果

    public DeferredResult<String> getAsyncUpdate(){
        deferredResult = new DeferredResult<String>();
        return deferredResult;
    }

    @Scheduled(fixedDelay = 5000)
    public void refresh(){
        if(deferredResult != null){
            deferredResult.setResult(new Long(System.currentTimeMillis()).toString());
        }
    }
}
